import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React from 'react';
import { Controlled as ControlledCodeMirror } from 'react-codemirror2';
import classnames from 'classnames';
import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/dracula.css';
import style from "./index.less?modules";

var CodeMirror = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(CodeMirror, _React$PureComponent);

  var _super = _createSuper(CodeMirror);

  function CodeMirror(props) {
    var _this;

    _classCallCheck(this, CodeMirror);

    _this = _super.call(this, props);
    var value = props.codeMirrorProps.value;
    _this.editor = null;
    _this.state = {
      code: value || ''
    };
    return _this;
  } // eslint-disable-next-line


  _createClass(CodeMirror, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var code = this.state.code;
      var value = nextProps.codeMirrorProps.value;

      if (code !== value) {
        this.setState({
          code: value
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$codeMirro = _this$props.codeMirrorProps,
          codeMirrorProps = _this$props$codeMirro === void 0 ? {} : _this$props$codeMirro,
          _this$props$fetchCode = _this$props.fetchCodeMirror,
          fetchCodeMirror = _this$props$fetchCode === void 0 ? function (e) {
        return e;
      } : _this$props$fetchCode;
      var _codeMirrorProps$opti = codeMirrorProps.options,
          options = _codeMirrorProps$opti === void 0 ? {} : _codeMirrorProps$opti,
          className = codeMirrorProps.className;

      var initProps = _objectSpread(_objectSpread({
        autoScroll: true,
        className: classnames(style['cpaas-codemirror'], className)
      }, codeMirrorProps), {}, {
        value: this.state.code,
        options: _objectSpread({
          lineNumbers: true,
          mode: 'yaml',
          autoFocus: true,
          cursorHeight: 0.85,
          viewportMargin: Infinity
        }, options)
      });

      return /*#__PURE__*/React.createElement(ControlledCodeMirror, Object.assign({}, initProps, {
        onBeforeChange: function onBeforeChange(editor, data, value) {
          _this2.setState({
            code: value
          });
        },
        editorDidMount: function editorDidMount(editor) {
          fetchCodeMirror(editor);
          _this2.editor = editor;
          editor.setSize('auto', '500px');
        }
      }));
    }
  }]);

  return CodeMirror;
}(React.PureComponent);

export { CodeMirror as default };